#include "LogManager.h"

namespace egg {

    LogManager::LogManager() {
        mDefaultLog=createLog("egg.log",false,true);
    }

    LogManager::~LogManager() {
		LogMap::iterator itr=mLogs.begin();
		for(;itr!=mLogs.end();itr++){
			delete itr->second;
		}
		mLogs.clear();
    }

    Log* LogManager::createLog(const string& name, bool debugOutput, bool fileOutput) {
        LogMap::iterator itr=mLogs.find(name);
        if(itr!=mLogs.end()){
            mDefaultLog->logMessage(name+" log already exist");
            return 0;
        }else{
            Log* newLog=new Log(name, debugOutput, fileOutput);
            mLogs.insert(make_pair(name,newLog));
            return newLog;
        }
    }

    void LogManager::setDefault(Log* log){
        mDefaultLog=log;
    }

	void LogManager::logMessage(const string& message){
		mDefaultLog->logMessage(message);
	}

    Log* LogManager::getLog(const string& name) {
		LogMap::iterator itr=mLogs.find(name);
		if(itr!=mLogs.end())
			return itr->second;
		else
			return 0;
    }

    void LogManager::toggle(bool t) {
		LogMap::iterator itr=mLogs.begin();
		for(;itr!=mLogs.end();itr++){
			itr->second->toggle(t);
		}
    }

    void LogManager::destroyLog(const string& name) {
		LogMap::iterator itr=mLogs.find(name);
		if(itr!=mLogs.end()){
			delete itr->second;
			mLogs.erase(itr);
		}
    }
	
}
